package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope  //配置热更新  对应@Value注解注入
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${server.port}")
    int port;

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id,
                          @RequestHeader(value ="name",required = false) String name) throws InterruptedException {

        if (id==1){
            //id为1，触发慢调用
            Thread.sleep(60);
        }else if (id==2){
            throw new RuntimeException("我故意的");
        }

        System.out.println("请求头name的值："+name);
        User user = userService.queryById(id);
        user.setAddress(port+"");
        return user ;
    }


//    @Value("${pattern.dateformat}")
//    private String dateformat;

//    @GetMapping("now")
//    public String now(){
//        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
//    }

    @Autowired
    private PatternProperties patternProperties;

    @GetMapping("now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }

    @GetMapping("prop")
    public PatternProperties prop(){
        return patternProperties;
    }
}
